package com.example.dadong.checkwordview;

import android.Manifest;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.library.RequestBack;
import com.example.library.RequestKnowledgeEntity;
import com.example.library.RequestKnowledgeParseEntity;
import com.example.library.dialog.KnowLedgeAnalysisDialog;
import com.example.library.http.KnowledgeEntity;
import com.example.library.http.KnowledgeParseBaseUrl;
import com.example.library.http.KnowledgeParseEntity;
import com.example.library.view.NewDealDataUtil;
import com.example.library.viewmodle.CheckWordDialogViewModel;
import com.lancoo.evaluate.util.permission.PermissionsManager;
import com.lancoo.evaluate.util.permission.PermissionsResultAction;

import java.util.ArrayList;
import java.util.List;

/**
 * 李继军
 */
public class KnowledgeActivity extends AppCompatActivity {

    private static final String TXT = "I'm still." +
            " very much figuring out what." +
            " I want to do with my life,I'm still." +
            " very much figuring out what." +
            " I want to do with my life";

    private boolean mHasAllPermissions;
    private TextView mTv;
    private List<KnowledgeEntity> mBuffer = new ArrayList<>();
    private KnowLedgeAnalysisDialog mDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_knowledge);

        mTv = findViewById(R.id.tv);

        requestAllPermissions();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mBuffer.clear();
    }

    private void requestAllPermissions() {
        PermissionsManager.getInstance().requestPermissionsIfNecessaryForResult(
                this,
                new String[]{
                        Manifest.permission.RECORD_AUDIO,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.READ_EXTERNAL_STORAGE
                },
                new PermissionsResultAction() {
                    @Override
                    public void onGranted() {
                        mHasAllPermissions = true;
                        KnowledgeParseBaseUrl.setBaseUrl("http://60.190.136.238:2048/");
                    }

                    @Override
                    public void onDenied(String permission) {
                    }
                });
    }

    public void onClick(View v) {
        if (!mHasAllPermissions) return;
        int id = v.getId();
        if (id == R.id.getBtn) {
            v.setClickable(false);
            get();
        } else if (id == R.id.openBtn) {
            if (mBuffer.isEmpty()) return;
            open();
        }
    }

    private void get() {
        new RequestKnowledgeEntity(this, new RequestBack<List<KnowledgeEntity>>() {
            @Override
            public void onSuccess(List<KnowledgeEntity> knowledgeEntities) {
                /**
                 * KnowledgeEntity（知识点）结构:
                 *
                 * ZSDCode: 知识点编码
                 * ZSDName: 知识点名字
                 * ZSDNameInText: 知识点在原文中是怎么展示的，比如: play，可能有played这样的形式，played就是ZSDNameInText，play就是ZSDName
                 */

                StringBuilder sb = new StringBuilder();
                sb.append("原文: ");
                sb.append(TXT);
                sb.append("\n");
                for (KnowledgeEntity entity : knowledgeEntities) {
                    sb.append("\n----------------------------------------------");
                    sb.append("\n知识点编码: ");
                    sb.append(entity.getZSDCode());
                    sb.append("\n知识点名字: ");
                    sb.append(entity.getZSDName());
                    sb.append("\n知识点的原文展示: ");
                    sb.append(entity.getZSDNameInText());
                }
                mTv.setText(sb.toString());

                mBuffer.clear();
                mBuffer.addAll(knowledgeEntities);
            }

            @Override
            public void onErr(Throwable e) {
                Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
            }
        }).getData(TXT, "B");// leveCode: A小学 B初中 C高中

    }

    //接口调整，不再根据token获取知识点
    private void open() {
        new RequestKnowledgeParseEntity(this, new RequestBack<KnowledgeParseEntity>() {
            @Override
            public void onSuccess(KnowledgeParseEntity knowledgeParseEntity) {
                showDialog(getViewModel(knowledgeParseEntity));
            }

            @Override
            public void onErr(Throwable e) {
                Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
            }
        }).getData(mBuffer.get(0).getZSDCode(), "B", "S2-666-E421","B","restu13");
    }

    private void showDialog(CheckWordDialogViewModel viewModel) {
        if (mDialog == null)
            mDialog = new KnowLedgeAnalysisDialog();
        mDialog.setMViewModel(viewModel);
        mDialog.show(getSupportFragmentManager());
    }

    private CheckWordDialogViewModel getViewModel(KnowledgeParseEntity knowledgeParseEntity) {
        CheckWordDialogViewModel viewModel = new CheckWordDialogViewModel();
        return NewDealDataUtil.getViewModel(viewModel, knowledgeParseEntity);
    }
}
